/**
 * 
 */
package nus.mtech.presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import nus.mtech.ApplicationFacade;
import nus.mtech.bill.Account;
import nus.mtech.bill.AccountManager;
import nus.mtech.constant.PresentationConstant;
import nus.mtech.customer.Customer;
import nus.mtech.customer.Complaint;
import nus.mtech.customer.CustomerManager;
import nus.mtech.customer.ComplaintManager;
import nus.mtech.util.DateUtil;
import nus.mtech.util.IntegerTextField;
import nus.mtech.util.StringUtil;

/**
 * @author Alvin Lee
 * 
 */
final class CustomerPanel extends JPanel implements ActionListener {

	private Dimension screen = null;
	private static volatile CustomerPanel INSTANCE;
	private JTextField txtNric;	
	private JButton btnSearch;
	private JButton btnClear;
	private JButton btnUpdate;
	private JButton btnComplaint;
	private JTextField txtName;
	private JTextField txtBillAddress1;
	private JTextField txtBillAddress2;
	private JTextField txtContact;
	private JTextField txtAccountNo;
	private JComboBox comboBoxStatus;
	private JLabel lblInterests;
	private JTextField txtInterests;
	private static final String ACTIVE_STATUS = "Active";
	private static final String INACTIVE_STATUS = "Disabled";
	
	//should move into AppFacade later
	private CustomerManager custMgr = CustomerManager.getCustomerManagerInstance();
	private AccountManager accountMgr = AccountManager.getInstance();
	
	
	CustomerPanel() {
		super(null);
		this.setBackground(PresentationConstant.CONSOLE_BACKGROUND);
		this.setPreferredSize(new Dimension(1004, 643));
		initialization();

	}

	public void initialization() {
		try {
			removeAll();

			screen = Toolkit.getDefaultToolkit().getScreenSize();

			final JLabel customerPanelLabel = new JLabel();
			customerPanelLabel.setHorizontalAlignment(SwingConstants.CENTER);
			customerPanelLabel.setFont(new Font("", Font.BOLD, 16));
			customerPanelLabel.setText("Manage Customer");
			customerPanelLabel.setBounds(0, 10, 1004, 21);
			add(customerPanelLabel);

			JLabel borderBase = new JLabel();
			borderBase.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
			borderBase.setBounds(40, 71, 944, 513);
			add(borderBase);

			JLabel lblCustomerIC = new JLabel("NRIC/Passport No:");
			lblCustomerIC.setBounds(90, 133, 106, 14);
			add(lblCustomerIC);

			txtNric = new JTextField();
			txtNric.setBounds(206, 130, 316, 20);
			add(txtNric);
			txtNric.setColumns(10);

			JLabel lblName = new JLabel("Name:");
			lblName.setBounds(90, 185, 106, 14);
			add(lblName);

			btnSearch = new JButton("Search");
			btnSearch.setBounds(579, 129, 89, 23);
			btnSearch.addActionListener(this);
			add(btnSearch);

			btnClear = new JButton("Clear");
			btnClear.setBounds(263, 544, 89, 23);
			btnClear.addActionListener(this);
			add(btnClear);

			JLabel lblCustomerInfo = new JLabel("Customer Information");
			lblCustomerInfo.setFont(new Font("Dialog", Font.BOLD, 14));
			lblCustomerInfo.setBounds(90, 88, 242, 21);
			add(lblCustomerInfo);

			txtName = new JTextField();
			txtName.setColumns(10);
			txtName.setBounds(206, 182, 316, 20);
			add(txtName);

			JLabel lblBillAddress = new JLabel("Billing Address:");
			lblBillAddress.setBounds(90, 232, 106, 14);
			add(lblBillAddress);

			txtBillAddress1 = new JTextField();
			txtBillAddress1.setColumns(10);
			txtBillAddress1.setBounds(206, 229, 316, 20);
			add(txtBillAddress1);

			txtBillAddress2 = new JTextField();
			txtBillAddress2.setColumns(10);
			txtBillAddress2.setBounds(206, 260, 316, 20);
			add(txtBillAddress2);

			JLabel lblTel = new JLabel("Contact No:");
			lblTel.setBounds(90, 308, 106, 14);
			add(lblTel);

			txtContact = new IntegerTextField();
			txtContact.setColumns(10);
			txtContact.setBounds(206, 305, 158, 20);
			add(txtContact);

			JLabel lblAccountID = new JLabel("Account No:");
			lblAccountID.setBounds(90, 400, 106, 14);
			add(lblAccountID);

			txtAccountNo = new JTextField();
			txtAccountNo.setEditable(false);
			txtAccountNo.setColumns(10);
			txtAccountNo.setBounds(206, 397, 158, 20);
			add(txtAccountNo);

			JLabel lblStatus = new JLabel("Status:");
			lblStatus.setBounds(90, 448, 106, 14);
			add(lblStatus);

			comboBoxStatus = new JComboBox();
			comboBoxStatus.setBounds(208, 445, 137, 20);
			comboBoxStatus.addItem(ACTIVE_STATUS);
			comboBoxStatus.addItem(INACTIVE_STATUS);
			add(comboBoxStatus);
			comboBoxStatus.setSelectedItem(ACTIVE_STATUS);

			btnUpdate = new JButton("Update / Add");
			btnUpdate.setBounds(123, 544, 106, 23);
			btnUpdate.addActionListener(this);
			btnUpdate.setEnabled(true);
			add(btnUpdate);

			btnComplaint = new JButton("View Complaints");
			btnComplaint.setBounds(377, 544, 145, 23);
			btnComplaint.addActionListener(this);
			add(btnComplaint);

			lblInterests = new JLabel("Interests:");
			lblInterests.setBounds(90, 358, 106, 14);
			add(lblInterests);

			txtInterests = new JTextField();
			txtInterests.setColumns(10);
			txtInterests.setBounds(206, 355, 316, 20);
			add(txtInterests);

		} catch (Exception ex) {
			System.out.println("exception");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		if (ae.getSource() == btnSearch) {
			// perform Search Actions
			if (txtNric.getText().length() == 0) {
				JOptionPane.showMessageDialog(this,
						"NRIC/Passport Information Required.",
						"Please enter NRIC or Passport No",
						JOptionPane.ERROR_MESSAGE);
				return;
			} else {
				// Perform Search
				Customer tempCust = ApplicationFacade.getInstance().getCustomerByNRIC(txtNric
						.getText().trim().toUpperCase());
				if (tempCust != null) {
					txtName.setText(tempCust.getCustName());
					String[] tempAddress = tempCust.getCustBillingAddr().split(
							";");
					txtBillAddress1.setText(tempAddress[0]);
					if (tempAddress.length > 1) {
						txtBillAddress2.setText(tempAddress[1]);
					}
					txtContact.setText(tempCust.getCustTelNo());
					txtInterests.setText(tempCust.getCustInterests());
					txtAccountNo.setText(tempCust.getAccountNo());
					if (tempCust.getCustStatus()) {
						comboBoxStatus.setSelectedItem(ACTIVE_STATUS);
					} else {
						comboBoxStatus.setSelectedItem(INACTIVE_STATUS);
					}
					// Update Complaint Tab if user is found
					//Console.setComplaintPanel.setAccountNo(txtAccountNo.getText());
					//Console.setComplaintPanel.populateComplaintData();
				} else {
					JOptionPane.showMessageDialog(this, "Customer not found.",
							"Customer not found", JOptionPane.ERROR_MESSAGE);
					clear();
					
					//Clear the view on complaint panel
					Console.setComplaintPanel.clearComboBox();
					Console.setComplaintPanel.clearComplaintData();
					Console.setComplaintPanel.clearAccountNoText();
					return;
				}
			}
		} else if (ae.getSource() == btnClear) {
			clear();
			
		} else if (ae.getSource() == btnComplaint) {
			Console.toggleCustomerSelectedPane(1);
			
			//Populate complaint data
			Console.setComplaintPanel.setAccountNo(txtAccountNo.getText());
			Console.setComplaintPanel.populateComplaintData();
			
			} else if (ae.getSource() == btnUpdate) {
			// Check if all fields are entered
			if (txtNric.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this,
						"NRIC/Passport Information Required.",
						"Please enter NRIC or Passport No",
						JOptionPane.ERROR_MESSAGE);
				return;
			} else if (txtName.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this, "Name Required.",
						"Please enter Name", JOptionPane.ERROR_MESSAGE);
				return;
			} else if (txtBillAddress1.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this, "Address Required.",
						"Please enter Address", JOptionPane.ERROR_MESSAGE);
				return;
			} else if (txtBillAddress1.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this, "Address Required.",
						"Please enter Address", JOptionPane.ERROR_MESSAGE);
				return;
			} else if (txtContact.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this, "Telephone No Required.",
						"Please enter Contact No", JOptionPane.ERROR_MESSAGE);
				return;
			} 

			Customer tempCust = new Customer(txtName.getText().trim(), txtNric
					.getText().trim().toUpperCase(), txtBillAddress1.getText()
					.trim()
					+ (StringUtil.isNotBlank(txtBillAddress2.getText())?";":"") + txtBillAddress2.getText().trim(), txtContact
					.getText(), txtInterests.getText().trim());
			// Continue to process
			if (CustomerManager.customerExists(txtNric.getText().trim().toUpperCase())) {
				// Perform Update
				if (INACTIVE_STATUS.equals(comboBoxStatus.getSelectedItem())) {
					tempCust.setCustStatus(false);
				}
				tempCust.setAccountNo(txtAccountNo.getText());
				ApplicationFacade.getInstance().updateCustomerDetails(tempCust);
				JOptionPane.showMessageDialog(this, "Customer Updated.",
						"Customer Information Updated",
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				
				//Perform Add New Customer
				int custID = custMgr.getCustomerId();
		
				//Modified by Joe [5-Apr-2011]
				//Start - Create account using AccountManager
				Account account = accountMgr.createAccount();
				account.setCustomerId(Integer.toString(custID));
				String tempAccountNo = account.getAccountNo();
				//End - Create account using AccountManager
				
				//Set accountNo into customer object
				tempCust.setAccountNo(tempAccountNo);
				
				//Add new customer
				ApplicationFacade.getInstance().addNewCustomer(tempCust);
				txtAccountNo.setText(tempAccountNo);
				JOptionPane.showMessageDialog(this, "New Customer Added.",
						"New Customer Information Added",
						JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}

	public void clear() {
		txtNric.setText("");
		txtName.setText("");
		txtBillAddress1.setText("");
		txtBillAddress2.setText("");
		txtAccountNo.setText("");
		txtContact.setText("");
		txtInterests.setText("");
		comboBoxStatus.setSelectedIndex(0);
	}

	public void setAdminFunction(boolean enable) {
		btnUpdate.setEnabled(enable);
	}

	public static CustomerPanel getInstance() {
		synchronized (CustomerPanel.class) {
			if (INSTANCE == null) {
				INSTANCE = new CustomerPanel();
			}
			return INSTANCE;
		}
	}

	public String getAccountNo(){
		return this.txtAccountNo.getText();
	}
}
